// subpkg/goods-detail/goods-detail.js
import { getGoodsDetail } from '../../api/goods'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsDetail: null,
    bannerList: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('---options---', options)
    this.getGoodsDetailData(options.proid)

    const shareUserId = options.shareUserId
    if (shareUserId) {
      // 给分享的那个用户，给他一点钱
    }
  },
  // onReady() {
  //   wx.showShareMenu({
  //     withShareTicket: true,
  //     menus: ['shareAppMessage', 'shareTimeline']
  //    })
  // },
  async getGoodsDetailData(proid) {
    const res = await getGoodsDetail(proid)

    wx.setNavigationBarTitle({
      title: res.data.proname,
    })

    this.setData({
      goodsDetail: res.data,
      bannerList: res.data.banners[0].split(',')
    })
  },
  preview(e) {
    wx.previewImage({
      urls: this.data.bannerList,
      current: e.currentTarget.dataset.image
    })
  },
  /**
   * 用户点击右上角分享（分享给好友、群）
   */
  onShareAppMessage(obj) {
    // 可以自定义分享的内容（默认分享当前内容的截图）
    console.log('----xxx---', obj)
    if (obj.from === 'button' && obj.target.dataset.type === 'friend') {
      return {
        title: '按钮的分享哦',
        path: `/pages/cart/cart?proid=${this.data.goodsDetail.proid}&shareUserId=98112231`,
        imageUrl: 'https://img0.baidu.com/it/u=2636242197,3930133883&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750'
      }
    } else if (obj.from === 'menu') {
      return {
        title: '菜单分享',
        path: `/pages/cart/cart?proid=${this.data.goodsDetail.proid}&shareUserId=98112231`,
        imageUrl: 'https://img1.baidu.com/it/u=4163046217,449490696&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281'
      }
    }
  },
  onShareTimeline() {
    console.log('----onShareTimeline---')
    return {
      title: '嗨购商城',
      query: `proid=${this.data.goodsDetail.proid}&shareUserId=98112231&name=lhr`,
      imageUrl: 'https://img2.baidu.com/it/u=2947861513,3006870117&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1167'
    }
  },
  shareTimeline() {
    console.log('---shareTimeline---')
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  goToCart() {
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  }
})